package za.org.jcicada.genss.sensor.impl;

import java.net.URISyntaxException;
import java.util.Observable;

import za.org.jcicada.genss.sensor.ISensorValue;
import za.org.jcicada.genss.sensor.SensorURN;

/**
 * Sensor sampling strategy that fires samples when the status or value of the
 * sensor changes (ie is not equal to the previous value).
 * @author rcrida
 */
public class StrategyEvent extends StrategyBase {

	private static final long serialVersionUID = 1L;
    private Object lastValue;
    
    /**
     * Construct event strategy and initialise status and value so that on update
     * it will immediately notify.
     * @param sensorURN the sensor URI
     */
    public StrategyEvent(SensorURN sensorURN) {
        super(sensorURN);
        lastValue = null;
    }
    
    /**
     * Construct strategy from string array
     * @param args created by toStringArray
     * @throws URISyntaxException
     */
    public StrategyEvent(String[] args) throws URISyntaxException {
        this(new SensorURN(args[0]));
    }
    
    /**
     * If either the status or value has changed then notify.
     * @param o the sensor that is being observed
     * @param arg ignored
     */
    @Override
    protected void updateValue(Observable o, ISensorValue value) {
        if (!value.getValue().equals(lastValue)) {
            lastValue = value.getValue();
            notifySample(value);
        }
    }

    public String[] toStringArray() {
        return new String[]{getSensorURN().toString()};
    }

}
